package org.bdware.doip.core.doipMessage;

import org.bdware.doip.core.crypto.GlobalCertifications;
import org.bdware.doip.core.model.digitalObject.DigitalObject;

import java.net.InetSocketAddress;
import java.security.PublicKey;

public class DoipMessageFactory {
        public static class DoipMessageBuilder {
            DoipMessage ret;

            public DoipMessageBuilder() {};

            public DoipMessageBuilder createRequest(String targetID, String operation) {
                ret = new DoipMessage(targetID,operation);
                ret.header.setIsRequest(true);
                return this;
            }

            public DoipMessageBuilder createResponse(DoipResponseCode responseCode, DoipMessage reqMsg) {
                if(reqMsg == null){
                    ret = new DoipMessage(null,null, 0);
                    return this;
                }

                ret = new DoipMessage(reqMsg.header.parameters.id,reqMsg.header.parameters.operation);
                ret.header.setIsRequest(false);
                ret.header.parameters = reqMsg.header.parameters;
                ret.header.parameters.response = responseCode;
                ret.requestID = reqMsg.requestID;
                ret.header.setIsCertified(reqMsg.header.isCertified());
                ret.setSender(reqMsg.getSender());
                ret.header.setIsEncrypted(reqMsg.header.isEncrypted());

                if(reqMsg.credential!= null && reqMsg.credential.getSigner() != null){
                    ret.setRecipientID(reqMsg.credential.getSigner());
                }

                return this;
            }

            public DoipMessageBuilder addAttributes(String key, String value){
                ret.header.parameters.addAttribute(key,value);
                return this;
            }

            public DoipMessageBuilder addAttributes(String key, int value){
                ret.header.parameters.addAttribute(key,value);
                return this;
            }

            public DoipMessageBuilder setBody(byte[] body) {
                ret.body.encodedData = body;
                ret.header.bodyLength = ret.body.getLength();
                return this;
            }

            public DoipMessageBuilder setBody(DigitalObject digitalObject) {
                ret.body.setDataAsDigitalObject(digitalObject);
                return this;
            }

            public DoipMessageBuilder setSender(InetSocketAddress inetSocketAddress) {
                ret.setSender(inetSocketAddress);
                return this;
            }

            public DoipMessage create() {
                return ret;
            }

            public DoipMessageBuilder setRequestID(int requestID){
                ret.requestID = requestID;
                return this;
            }
        }

        public static DoipMessage createTimeoutResponse(int requestID) {
            DoipMessage ret = new DoipMessage(null,null);
            ret.header.IsRequest = true;
            ret.header.parameters.response = DoipResponseCode.UnKnownError;
            ret.header.parameters.addAttribute("timeout","timeout");
            ret.requestID = requestID;
            return ret;
        }

        public static DoipMessage createConnectFailedResponse(int requestID){
            DoipMessage ret = new DoipMessage(null,null);
            ret.header.IsRequest = true;
            ret.header.parameters.response = DoipResponseCode.UnKnownError;
            ret.header.parameters.addAttribute("failed","connect target doip service failed");
            ret.requestID = requestID;
            return ret;
        }
}
